package jackson.src.day04.reflect;

import java.lang.reflect.Field;

/*
 * Author: JacksonCoder
 * FileName: callFields
 * Version: 1.0.0
 */
public class callFields {
    public static void main(String[] args) throws Exception {
        Class stdClass = Student.class;
        // 获取public 字段 "score"
        System.out.println(stdClass.getField("score"));
        // private 字段 "grade"
        System.out.println(stdClass.getField("name"));
        // 获取继承 public字段 name
        System.out.println(stdClass.getDeclaredField("grade"));

        Person p = new Person("Xiao Ming");
        System.out.println(p.getName());
        Class c = p.getClass();
        Field f = c.getDeclaredField("name");
        f.setAccessible(true);   //  修改非public字段，需要首先调用setAccessible(true)。
        f.set(p,"Xiao Hong");
        System.out.println(p.getName());

    }
}

class Student extends Person{
    public int score;
    private int grade;

    public Student(String name) {
        super(name);
    }
}
class Person{
    public String name;

    public Person(String name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }


}